package com.example.administrator.easy.detailxinfo;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsListView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.easy.R;

import org.xutils.x;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class DetailFuli extends AppCompatActivity {
    private WebView webView;
    private TextView titles;
    private String title="宝贝详情";
    private int id;
    private String img;
    private CheckBox box;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_detail_fuli);
        
        initvew();
        setDeta();
    }

    private void initvew() {
        webView= (WebView) findViewById(R.id.layout_box_fuli);
        titles= (TextView) findViewById(R.id.detail_fuli_title);
        box= (CheckBox) findViewById(R.id.detail_ck);
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    Toast.makeText(DetailFuli.this, "已收藏", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(DetailFuli.this, "取消收藏", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void setDeta() {
        Intent intent=getIntent();
        String text = intent.getStringExtra("content");
        String titl = intent.getStringExtra("title");
        id=intent.getIntExtra("id",48557);
        img = intent.getStringExtra("img");
        if (!TextUtils.isEmpty(titl)){
            title = titl;
        }




       //替换html文本内容，设置图片自适应屏幕
        String s = text.replaceAll("<img", "<img width='100%' style=/\"word-wrap:break-word; font-family:Arial ");

//        String s1 = s.replaceAll("<source", "<source width='100%' style=/\"word-wrap:break-word; font-family:Arial ");
        webView.loadDataWithBaseURL(null,s,"text/html","UTF-8",null);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.setWebViewClient(new WebViewClient());

        webView.requestFocus();
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }
        });

        titles.setText(title);


    }

    public void click(View view) {

        switch (view.getId()){
            case R.id.fuli_share:
                showShare();
                break;
            case R.id.detail_discover_lv_back:
                finish();
                break;

               default:
                break;
        }

    }




    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("Easy :"+title);
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://m.ieasy.tv/api/easyarticledetail.html?id="+id);
        // text是分享文本，所有平台都需要这个字段
        oks.setText("Easy "+title);
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(img);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://m.ieasy.tv/api/easyarticledetail.html?id="+id);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("Easy "+title);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("Easy···");
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://m.ieasy.tv/api/easyarticledetail.html?id="+id);

// 启动分享GUI
        oks.show(this);
    }
}
